package com.firstappdemo.repository;

import com.firstappdemo.domain.User;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * {@link User, @link Repository}
 */
@Repository
public class UserRepository {

    private final ConcurrentMap<Integer, User> repository = new ConcurrentHashMap<>();

    private AtomicInteger idGen = new AtomicInteger();

    /**
     * @param user {@link User} 对象
     * @return
     */
    public boolean save(User user) {
        boolean success = false;
        Integer id = idGen.incrementAndGet();
        user.setId(id);
        return repository.put(id, user) == null;
    }

    /**
     * @return
     */
    public Collection<User> allPerson() {
        return repository.values();
    }

}
